#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <queue>
using namespace std;
int c[] = { 0,0,1,-1 };
int d[] = { 1,-1,0,0 };
using pii = pair<int, int>;
class Solution {
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color)
    {
        int m = image.size(), n = image[0].size();
        int pix = image[sr][sc];
        if (pix == color) return image;
        queue<pii> q;
        q.push({ sr,sc });
        while (!q.empty())
        {
            int a= q.front().first;
            int b = q.front().second;
            q.pop();
            image[a][b] = color;
            for (int i = 0;i < 4;i++)
            {
                int x = a + c[i], y = b + d[i];
                if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == pix)
                {
                    q.push({ x,y });
                }
            }
        }
        return image;
    }
};